package me.sort;

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {6, 4, 3, 5, 1, 2};
        sort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        System.out.println("----------------------");
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr) {
        bubble(arr, arr.length - 1);
    }

    public static void bubble(int[] arr, int j) {
        // j表示未排序区域的右边界
        // 递归出口
        if (j == 0) {
            return;
        }
        for (int i = 0; i < j; i++) {
            if (arr[i] > arr[i + 1]) {
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        bubble(arr, j - 1);
    }
}
